#import "AssetCollectionViewNode.h"
#import "ImageAsset.h"
#import "SlideCarrierView.h"

@implementation AssetCollectionViewNode

- (void)loadUIFromNib {
    // Load our views from the "SlideTableNode" nib file.
    [NSBundle loadNibNamed:@"SlideTableNode" owner:self];

    // Make our imageView partly transparent to give a slide-like appearance.  (A view's alphaValue can also be set using the new inspectors in Interface Builder 3.0)
    [imageView setAlphaValue:0.9];
}

- (void)dealloc {
    [self setAsset:nil];
    [super dealloc];
}

- (Asset *)asset {
    return asset;
}

- (void)setAsset:(Asset *)newAsset {
    if (asset != newAsset) {
        // Release our old asset and retain the new one we've been assigned.
        id old = asset;
        asset = [newAsset retain];
        [old release];

        // Load our UI from SlideTableNode.nib, if we haven't already.
        if (imageView == nil) [self loadUIFromNib];

        // Check whether we've loaded a preview image for the asset.  If so, assign the preview image to the slide view's image view.  If it hasn't been loaded yet, ask for it to be loaded (asyncrhonously), and we'll pick it up later.
        NSImage *previewImage = [asset previewImage];
        if (previewImage) {
            [imageView setImage:[asset previewImage]];
        } else {
            [asset requestPreviewImage];
        }

        // Associate our SlideCarrierView with its assigned asset.
        [rootView setAsset:asset];
    }
}

- (NSView *)rootView {
    // Asking an AssetCollectionViewNode for its "rootView" causes its view tree to be loaded from SlideTableNode.nib the first time it's asked for.
    if (rootView == nil) [self loadUIFromNib];
    return rootView;
}

@end
